package com.jxb.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/9/16 17:51
 **/
@Configuration
public class QueueDLXConfig {

    //todo 死信队列名称
    public static final String QUEUEDLX_NAME = "queuedlx_name";
    //todo 死信交换机名称
    public static final String EXCHANGEDLX_NAME = "exchangedlx_name";
    //todo 普通队列名称
    public static final String QUEUE_NAME = "queue_name";


    //todo 普通队列
    @Bean(QUEUE_NAME)
    public Queue QUEUE_NAME(){
        return new Queue(QUEUE_NAME, true);
    }


    //todo 死信交换机
    @Bean(EXCHANGEDLX_NAME)
    public DirectExchange EXCHANGEDLX_NAME(){
        return new DirectExchange(EXCHANGEDLX_NAME);
    }


    //todo 死信交换机绑定普通队列
    @Bean
    public Binding bindingDLX(
            @Qualifier(EXCHANGEDLX_NAME) DirectExchange directExchange,
            @Qualifier(QUEUE_NAME)Queue queue){
        return BindingBuilder.bind(queue).to(directExchange).with(EXCHANGEDLX_NAME);
    }


    //todo 死信队列绑定死信交换机
    @Bean(QUEUEDLX_NAME)
    public Queue queueDLX() {
        return QueueBuilder.durable(QUEUEDLX_NAME)
                //#todo 这里是死信队列的特有特性，
                //#todo 具有该特性才可以进行转发给交换机
                //#todo "x-dead-letter-exchange" 这个参数是固定写死的
                .withArgument("x-dead-letter-exchange", EXCHANGEDLX_NAME)
                //#todo "x-dead-letter-routing-key" 这个参数也是固定写死的
                .withArgument("x-dead-letter-routing-key", EXCHANGEDLX_NAME).build();
    }

}
